package servlet;

import com.google.gson.Gson;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;


public abstract class BaseServlet extends HttpServlet {
    protected Gson mGson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //设置编码格式，可以防止乱码出现
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = response.getWriter();

        writer.write(responseJson());
        writer.flush();
        writer.close();
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }


    /**
     * 返回的json
     * @return
     */
    protected abstract String responseJson() throws UnsupportedEncodingException;
}
